package com.lyw.trans.controller;

import com.lyw.trans.service.LogService;
import com.lyw.trans.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@Slf4j
@RequestMapping("/user2")
@RestController
public class UserController2 {
    @Autowired
    private UserService userService;

    @Autowired
    private LogService logService;

    @Transactional
    @RequestMapping("/registry")
    public String registry(String name,String password) {
        //用户注册
        Integer result = userService.registryUser(name,password);
        log.info("用户注册成功, 影响行数:" + result);
        try {
            int a = 10/0;
        } catch (Exception e) {
            log.error("程序发生异常");
//            throw new RuntimeException(e);
            //手动回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }

        return "注册成功";
    }

    /**
     * 事务没有回滚
     * @param name
     * @param password
     * @return
     * @throws IOException
     */
    @Transactional
    @RequestMapping("/r2")
    public String r2(String name,String password) throws IOException {
        //用户注册
        Integer result = userService.registryUser(name,password);
        log.info("用户注册成功, 影响行数:" + result);
        if (true) {
            throw new IOException();
        }
        return "注册成功";
    }

    /**
     * 事务进行回滚
     * @param name
     * @param password
     * @return
     * @throws IOException
     */
    @Transactional(rollbackFor = {IOException.class, RuntimeException.class, Error.class})
    @RequestMapping("/r3")
    public String r3(String name,String password) throws IOException {
        //用户注册
        Integer result = userService.registryUser(name,password);
        log.info("用户注册成功, 影响行数:" + result);
        if (true) {
            throw new IOException();
        }
        return "注册成功";
    }


    @Transactional(isolation = Isolation.READ_COMMITTED)
    @RequestMapping("/r4")
    public String r4(String name,String password) throws IOException {
        //用户注册
        Integer result = userService.registryUser(name,password);
        log.info("用户注册成功, 影响行数:" + result);
        if (true){
            throw new IOException();
        }
        return "注册成功";
    }

    @Transactional
    @RequestMapping("/r5")
    public String r5(String name,String password) {
        //用户注册
        Integer result = userService.registryUser(name,password);
        log.info("用户注册成功, 影响行数:" + result);
        Integer logResult = logService.insertLog(name,"用户注册");
        log.info("日志记录成功, 影响行数:" + logResult);

        return "注册成功";
    }
}
